package com.lwm.service.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwm.domain.EarlyWarning;
import com.lwm.domain.ResponseResult;
import com.lwm.mapper.EarlyWarningMapper;
import com.lwm.service.EarlyWarningService;
import com.lwm.utils.Code;
import com.lwm.utils.HintMessage;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 能效预警业务层
 */
@Service
public class EarlyWarningServiceImpl implements EarlyWarningService {
    @Resource
    private EarlyWarningMapper earlyWarningMapper;

    /**
     * 保存
     *
     * @param earlyWarning 能效预警实体类
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> save(EarlyWarning earlyWarning) {
        // 必填参数 检测点名称、检测点地址、实时功率、实时电压、状态
        if (!StringUtils.hasText(earlyWarning.getMonitoringPointName())
                || !StringUtils.hasText(earlyWarning.getMonitoringPointAddress())
                || earlyWarning.getRealtimePower() == null
                || earlyWarning.getRealtimeVoltage() == null
                || earlyWarning.getStatus() == null
        ) {
            return new ResponseResult<>(Code.SAVE_ERR, HintMessage.MUST_PARAM_HINT);
        }
        earlyWarning.setId(IdUtil.getSnowflakeNextId());
        earlyWarningMapper.insert(earlyWarning);
        return new ResponseResult<>(Code.SAVE_OK, HintMessage.SAVE_OK_HINT);
    }

    /**
     * 删除
     *
     * @param ids id集合
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> remove(List<Long> ids) {
        if (ids.isEmpty()) {
            return new ResponseResult<>(Code.DELETE_ERR, HintMessage.REMOVE_LACK_ID + "监测点");
        }
        int i = earlyWarningMapper.deleteByIds(ids);
        if (i <= 0) return new ResponseResult<>(Code.DELETE_ERR, HintMessage.REMOVE_ERR_HINT);
        return new ResponseResult<>(Code.DELETE_OK, HintMessage.REMOVE_OK_HINT);
    }

    /**
     * 编辑
     *
     * @param earlyWarning 能效预警实体类
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> edit(EarlyWarning earlyWarning) {
        if (null == earlyWarning.getId()) {
            return new ResponseResult<>(Code.UPDATE_ERR, HintMessage.EDIT_LACK_ID + "监测点");
        }
        int i = earlyWarningMapper.update(earlyWarning);
        if (i <= 0) return new ResponseResult<>(Code.UPDATE_ERR, "修改失败");
        return new ResponseResult<>(Code.UPDATE_OK, "修改成功");
    }

    /**
     * 分页
     *
     * @param earlyWarning 能效预警实体类
     * @param page         页码
     * @param size         数量
     * @return {@link ResponseResult}<{@link PageInfo}<{@link EarlyWarning}>>
     */
    @Override
    public ResponseResult<PageInfo<EarlyWarning>> page(Integer page, Integer size, EarlyWarning earlyWarning) {
        // 设置默认值
        if (page == null) page = 1;
        if (size == null) size = 5;

        PageInfo<EarlyWarning> pageInfo = PageHelper
                .startPage(page, size)
                .doSelectPageInfo(() -> earlyWarningMapper.selectAll(earlyWarning));
        return new ResponseResult<>(Code.GET_OK, HintMessage.GET_OK_HINT, pageInfo);
    }

    /**
     * 根据id获取能效预警信息
     *
     * @param id 能效预警id
     * @return {@link ResponseResult}<{@link Object}>
     */
    @Override
    public ResponseResult<Object> getById(Long id) {
        if (null == id)
            return new ResponseResult<>(Code.GET_ERR, HintMessage.GET_LACK_ID + "监测点信息");
        EarlyWarning earlyWarning = earlyWarningMapper.selectById(id);
        if (Objects.isNull(earlyWarning)) return new ResponseResult<>(Code.GET_ERR, HintMessage.GET_ERR_HINT);
        return new ResponseResult<>(Code.GET_OK, HintMessage.GET_OK_HINT, earlyWarning);
    }
}
